#!/usr/bin/env python

'''
mm color gray decompose.py
Decompose the layer in two parts, to work separately on the gray values.

Author:
Michael Munzert (mail mm-log com)

Version:
2010.03.06 Inital release.

License:

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

The GNU Public License is available at
http://www.gnu.org/copyleft/gpl.html

'''

from gimpfu import *

def plugin_main(image, drawable, mode, visible):
  pdb.gimp_image_undo_group_start(image)

  if visible == 0:  
    # Copy and desaturate
    tempgraylayer = drawable.copy(True)
    tempgraylayer.mode = GRAIN_EXTRACT_MODE
    image.add_layer(tempgraylayer, -1)    
    if mode == 0:
      pdb.gimp_desaturate_full(tempgraylayer,0)
    elif mode == 1: 
      pdb.gimp_desaturate_full(tempgraylayer,1)
    else: 
      pdb.gimp_desaturate_full(tempgraylayer,2)
    # Copy again
    graylayer = tempgraylayer.copy(True)
    graylayer.mode = GRAIN_MERGE_MODE
    # Merge down for color layer
    colorlayer = pdb.gimp_image_merge_down(image, tempgraylayer, 0)
    colorlayer.mode = NORMAL_MODE
    colorlayer.name = "Color"
    # Add gray layer on top
    graylayer.name = "Luminance"
    image.add_layer(graylayer, -1)
  else:
    # Get visible
    newlayer = pdb.gimp_layer_new_from_visible(image, image, "Color")
    newlayer.mode = NORMAL_MODE
    image.add_layer(newlayer, 0)
    # Copy and desaturate
    tempgraylayer = newlayer.copy(True)
    tempgraylayer.mode = GRAIN_EXTRACT_MODE
    image.add_layer(tempgraylayer, 0)    
    if mode == 0:
      pdb.gimp_desaturate_full(tempgraylayer,0)
    elif mode == 1: 
      pdb.gimp_desaturate_full(tempgraylayer,1)
    else: 
      pdb.gimp_desaturate_full(tempgraylayer,2)
    # Copy again
    graylayer = tempgraylayer.copy(True)
    graylayer.mode = GRAIN_MERGE_MODE
    # Merge down for color layer
    colorlayer = pdb.gimp_image_merge_down(image, tempgraylayer, 0)
    colorlayer.mode = NORMAL_MODE
    # Add gray layer on top
    graylayer.name = "Luminance"
    image.add_layer(graylayer, 0)

  pdb.gimp_image_undo_group_end(image)
  gimp.displays_flush()
  
  
register(
        "python_fu_mm_color_gray_decompose",
        "Color gray decompose.",
        "Color gray decompose.",
        "Michael Munzert (mail mm-log com)",
        "Copyright 2010 Michael Munzert",
        "2010",
        "<Image>/Filters/MM-Filters/_Color Gray Decompose...",
        "*",
        [ (PF_OPTION,"mode", "Desaturation", 0, ["Lightness","Luminosity","Average"]),
          (PF_RADIO, "visible", "Layer:", 1, (("new from visible", 1),("current layer",0)))
        ],
        [],
        plugin_main,
        )

main()
 

